﻿using System;
using System.Collections.Generic;
using System.Linq;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Core.Configuration;
using Kevin.Shop.Business.Core.Enumerations;
using Kevin.Shop.Business.Core.Utility;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;

namespace RiderShop.Mobile.Common.Component
{
    public class SiteUrl 
    {
        public static string AuthToken;
        public static Dictionary<string, string> UrlList = new Dictionary<string, string>();
        public static Uri HostName;
        public static Uri MobileHostName;

        public static void Init()
        {
            string authTokenuser = System.Configuration.ConfigurationManager.AppSettings["DefaultAuthUser"];
            string authTokenPwd = System.Configuration.ConfigurationManager.AppSettings["DefaultAuthPwd"];
            HostName = new Uri(System.Configuration.ConfigurationManager.AppSettings["DefaultHost"]);
            MobileHostName = new Uri(System.Configuration.ConfigurationManager.AppSettings["MobileDefaultHost"]);
            var responser = RestfulHelper.UpdateWithQueryString(string.Format("{0}/UserToken.ashx?userName={1}&pwd={2}", HostName, authTokenuser, authTokenPwd));
            //set auth token
            AppContext.AppUserToken = responser.Tag; //
            //build url list
            var list = RestfulHelper.Query<Tab>(string.Format("{0}/Service/CommonServicer.svc/GetUrlList", HostName));
            foreach (var tab in list)
            {
                if (!UrlList.ContainsKey(tab.Name))
                {
                    UrlList.Add(tab.Name, tab.Href);
                }
            }
        }

        public static string FormatUrl(string key, params object[] parameters)
        {
            var url = parameters == null || parameters.Count() == 0 ? UrlList[key] : string.Format(UrlList[key], parameters);
            var defaultHost = HostName.Scheme + "://" + HostName.Authority;
            return !url.ToLower().Contains(defaultHost.ToLower()) ? string.Format("{0}{1}", defaultHost, url) : url;
        }

        /// <summary>
        /// 获取product image url
        /// </summary>
        public static string FormatCommImageUrl(UploadSettingType imageType, object imageName)
        {
            if (imageName != null)
            {
                string urlKey = string.Format("WebSite_Image_{0}", imageType);
                return FormatUrl(urlKey, ImageEntity.GetImagePrifx(imageName.ToString()), imageName);
            }
            return null;
        }

        /// <summary>
        /// 获取product image url,默认是180x180大小
        /// </summary>
        public static string FormatProductImageUrl(object productId, object imageName)
        {
            return FormatProductImageUrl(ImageType.Size180, productId, imageName);
        }

        /// <summary>
        /// 获取product image url
        /// </summary>
        public static string FormatProductImageUrl(ImageType size, object productId, object imageName)
        {
            string urlKy = string.Format("ProductManagement_ProductImage{0}", (int)size);
            if (imageName == null || imageName.ToString() == ProductBasicInfo.DefaultComingSoonImage)
            {
                urlKy = string.Format("ProductManagement_ProductImageComingSoon{0}", (int)size);
                return FormatUrl(urlKy);

            }
            string prifx = ImageEntity.GetProductImagePrifx(productId.ToString());
            return FormatUrl(urlKy, prifx, productId, imageName);

        }
    }
}
